package com.eascs.caseManagement.controller;

import static com.eascs.site.view.HtmlRestResult.popHtmlFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.admin.common.utils.StringUtils;
import com.eascs.app.dao.PageResult;
import com.eascs.caseManagement.entity.CaseManagement;
import com.eascs.caseManagement.entity.CaseManagementHis;
import com.eascs.caseManagement.form.CaseManageForm;
import com.eascs.caseManagement.service.CaseManageService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@RestController
@RequestMapping("/caseManageRest")
public class CaseManageRestController extends BaseRestController {
	
	@Inject
	CaseManageService caseManageService;
	
	@RequestMapping("/add")
	public BaseRestResult<String> viewInsert() {
		User user = UserHolder.getUser();
		ViewContext.put("currentUser", user.getCname());
		ViewContext.put("currentUserCode", user.getNo());
		return HtmlRestResult.popHtmlFragment("/caseManage/case_manage_add");
	}
	
	
	
	
	
	@RequestMapping("/edit")
	public BaseRestResult<String> viewUpdate(String id) {
		CaseManagement cm = caseManageService.getCaseManageById(id);
		ViewContext.put("vo", cm);
		User user = UserHolder.getUser();
		ViewContext.put("currentUser", user.getCname());
		ViewContext.put("currentUserCode", user.getNo());
		return HtmlRestResult.popHtmlFragment("/caseManage/case_manage_add");
	}

	
	@RequestMapping("/delete")
	public BaseRestResult<String> deleteBrand(String id) {
		boolean flag =caseManageService.updateCaseManageStatus(id);
		if(flag){
			return BaseRestResult.info("删除成功！").andRefresh();
		}else{
			return BaseRestResult.warn("删除失败！");
		}
	}
	
	
	@RequestMapping("/add_save")
	public BaseRestResult<String> addSave(CaseManagement vo) {
		BaseRestResult<String> result  = null;
		Map<String, String>    results = caseManageService.addSave(vo);
		String                 flag    = results.get("flag");
		if ("success".equals(flag)) {
			result = BaseRestResult.info("添加成功").andRefresh();
		} else if ("fail".equals(flag)) {
			result = BaseRestResult.error("操作失败");
		}
		return result;
	}
	
	
	
	@RequestMapping(value = "/alter_info_record")
	public PaginationRestResult alterInfoRecord(CaseManageForm vo) {
		String o2ourl = "/caseManageRest/alter_info_record?id=" + vo.getBillCode() + "&pageIndex=";
		vo.setPageSize(10);
		PageResult<Object[]> pageResult2     = caseManageService.caseManageHisList(vo);
		String[]             titles2         = { "贷款单号" , "修改时间" ,  "修改人"};
		PaginationInfo       paginationInfo2 = new PaginationInfo(pageResult2, titles2, o2ourl);
		AjaxButton abutton = new AjaxButton("详情", "/caseManageRest/detail?id=");
		Button[] morButtons = new Button[] { abutton};
		paginationInfo2.setBuildPagingNavigation(true);
		paginationInfo2.setFirstRenderColumnIndex(1);
		paginationInfo2.setMoreButtons(morButtons);
		paginationInfo2.setBuildRowNumber(true);
		paginationInfo2.setRendTo("#alter_record");
		ViewContext.put("paginationInfo2", paginationInfo2);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo2);
	}
	
	@RequestMapping(value = "/detail")
	public HtmlRestResult detailHis(String id) {
		CaseManagementHis cmh = caseManageService.getCaseManageHisById(id);
		ViewContext.put("vo", cmh);
		return popHtmlFragment("/caseManage/detailHis");
	}
	
	@RequestMapping("/searchByBillCodecommon")
	public BaseRestResult<List<Object[]>> searchByBillCodecommon(String code) {
		List<Object[]> objsList = new ArrayList<>();
		if (!StringUtils.isEmpty(code)) {
			objsList = caseManageService.findscommon(code);
		}
		return BaseRestResult.buildRestResult(objsList);
	}
}